package com.jic.finance.controller;


import com.alibaba.fastjson.JSONArray;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.common.redis.RedisClientTemplate;
import com.jic.finance.common.util.BeanUtil;
import com.jic.finance.entity.CurrencyManage;
import com.jic.finance.request.manager.CurrencyManageRequest;
import com.jic.finance.response.manager.CurrencyManageResponse;
import com.jic.finance.service.CurrencyManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: MJ
 * @date: 2020-01-09
 * @remark: 币种管理管理系统的服务
 */
@Api("币种管理")
@RestController
@RequestMapping("/currencyManage")
@Slf4j
public class CurrencyManageController extends BaseController{

    @Autowired
    private CurrencyManageService currencyManageService;


    @Autowired
    private RedisClientTemplate redisClientTemplate;


    /**
     * 分页查询
     * @param form
     * @return
     */
    @ApiOperation(value = "获取所有币种列表")
    @ResponseBody
    @RequestMapping("/queryList")
    public RestResult<PageResult<CurrencyManageResponse>> queryList(@RequestBody CurrencyManageRequest form) {
        log.info("开始获取所有币种列表=========》,{}", form.toString());
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        CurrencyManageRequest currencyManageRequest = BeanUtil.copy(form, CurrencyManageRequest.class);
        currencyManageRequest.setDeleteFlag(0);
        PageResult<CurrencyManage> result = currencyManageService.queryPage(currencyManageRequest, page);
        List<CurrencyManageResponse> currencyManageResponseList = result.getRows().stream().map(temp -> {
            CurrencyManageResponse paymentMethodResponse = BeanUtil.copy(temp, CurrencyManageResponse.class);
            return paymentMethodResponse;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<CurrencyManageResponse> pageResult = new PageResult<>();
        pageResult.setRows(currencyManageResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }



    /*
     * 新增/修改币种数据
     * */
    @ApiOperation(value = "新增/修改币种数据")
    @ResponseBody
    @RequestMapping("/editCurrencyManage")
    public RestResult<String> editCurrencyManage(@RequestBody CurrencyManageRequest form) {
        log.info("开始进入新增/修改币种数据=========》,{}", form.toString());
        RestResult restResult = new RestResult<>();
        try {
            if (StringUtils.isEmpty(form.getToken())){
                throw new Exception("必传参数为空 token");
            }
            String userStr = redisClientTemplate.get(form.getToken());
            if (StringUtils.isEmpty(userStr)){
                return RestResult.error("4000","登录失效");
            }
            Map sysUser = JSONArray.parseObject(userStr, Map.class);
            //修改
            if(null!=form.getId()&&!"".equals (form.getId())){
                form.setUpdater(Long.valueOf(sysUser.get("id").toString()));
                form.setUpdaterName(sysUser.get("username").toString());
                form.setDeptId(Long.valueOf(sysUser.get("deptId").toString()));
                long flag=currencyManageService.update(form);
                restResult=RestResult.success(restResult);
            }else {
                form.setDeleteFlag(0);
                form.setCreator(Long.valueOf(sysUser.get("id").toString()));
                form.setCreatorName(sysUser.get("username").toString());
                long flag=currencyManageService.insert(form);
                restResult=RestResult.success(restResult);
            }

        } catch (Exception e) {
            log.error("error",e);
            restResult=RestResult.error("9999",e.getLocalizedMessage());
        }
        return restResult;
    }


    /*
     * 删除币种数据
     * */
    @ApiOperation(value = "删除币种数据")
    @ResponseBody
    @RequestMapping("/deleteInfo")
    public RestResult<String> deleteInfo(@RequestBody CurrencyManageRequest form) {
        log.info("开始进入新增/删除币种数据=========》,{}", form);
        RestResult restResult = new RestResult<>();
        try {
            String[] checked = form.getIds().split(",");
            for(int i=0;i<checked.length;i++){
                CurrencyManage currencyManage = currencyManageService.queryByPrimaryKey(Long.valueOf(checked[i]));
                if (currencyManage.getSysPreset()==1){
                    restResult.setCode("9999");
                    restResult.setMsg("系统预设货币不得删除，请检查数据！");
                    return restResult;
                }
                currencyManageService.delete(Long.valueOf(checked[i]));
            }
            restResult.setCode("0000");
        } catch (Exception e) {
            log.error("error",e);
            restResult=RestResult.error("9999",e.getLocalizedMessage());
        }
        return restResult;
    }

}
